/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.utils;


import br.ufba.lasid.v1.jitclouds.mda.datatypes.BasicCluster;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.Resource;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.ResourceList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


/**
 *
 * @author marivaldorodrigues
 */
public class ReadFileForAssessment {

    /**
     * @param args the command line arguments
     */
    
//    List<String> clustersName = new ArrayList<String>(); // Names of the clusters
//    List<String> clustersAddress = new ArrayList<String>(); // Address of the clusters
    private List<BasicCluster> clusterList = new ArrayList<BasicCluster>(); //  List of the VM Types by clusters
    
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
       //String MDA_HOME= System.getenv("MDA_HOME");
        new ReadFileForAssessment("/Users/vinicius/Downloads/" + "azones.txt");  //Informar endereco do arquivo
    }     
    
    public ReadFileForAssessment(String pathFile) throws IOException {
        BasicCluster bCluster = new BasicCluster();
        List<Resource> r = new ArrayList<Resource>();
        ResourceList rL;
        String idCluster = null;
        String address = null;
        
        String line;
        List<String[]> lines = new ArrayList<String[]>();
        
        FileReader fileZones = new FileReader(pathFile);
        BufferedReader reader = new BufferedReader(fileZones);
          
        while ((line = reader.readLine()) != null) {
            String[] st = line.split("\t");
            lines.add(st);            
        }
        
        int n=0;
       // boolean b = false;
       // String[][] types = new String[5][3];
        
        for(int i=0; i<lines.size();i++) {
            if(i==0 || (i%7==0)){
                idCluster = lines.get(i)[1];
                address = lines.get(i)[2];
        //        b = true;
            }

            
            if( lines.get(i)[1].startsWith("|-") && !(lines.get(i)[1].endsWith("types")) ) {
                String[] types = lines.get(i)[1].split(" "); // take position 1 in type
//                String[] resources = lines.get(i)[2].split(" "); // take positions 0(free) and 2(max) in availability
                
                StringTokenizer st = new StringTokenizer(lines.get(i)[2], " ");
                
                String[] resources = new String[6];
                int k=0;
                    while(st.hasMoreTokens()) {
                        resources[k++] = st.nextToken();
                    }
                
//                String[] l = new String[3];
//                l[0]=type[1]; // type
//                l[1]=availability[0]; // free
//                l[2]=availability[2]; // max
                
                String type = types[1];
                Resource rs = new Resource();
                rs.setName(type);
                rs.setFree(Integer.parseInt(resources[0]));
                rs.setMax(Integer.parseInt(resources[2]));
                rs.setCpu(Integer.parseInt(resources[3]));
                rs.setRam(Integer.parseInt(resources[4]));
                rs.setDisk(Integer.parseInt(resources[5]));
                r.add(rs);
                n++;
                
                if(n==5) {
                    rL = new ResourceList();
                    rL.setVmTypes(r);
                    bCluster.setIdCluster(idCluster);
                    bCluster.setAddress(address);
                    bCluster.setResources(rL);
                    clusterList.add(bCluster);
                    bCluster.debug();
                    
                    bCluster = new BasicCluster();
                    n=0;
                    
                }
                
            }

        }
        

//        for(int i=0;i<this.clustersAddress.size();i++) { 
//            System.out.println(this.clustersName.get(i) + "\t" + this.clustersAddress.get(i));
//            
//            for(int j=0; j<5; j++) {
//                System.out.println("|- " + this.vmTypes.get(i)[j][0] + "\t" + this.vmTypes.get(i)[j][1] + " / " + this.vmTypes.get(i)[j][2]);
//            }            
//        }
        
    }
    
    public List<BasicCluster> getClusterList() {
        return clusterList;
    }
}